//
//  CommitTaskViewController.m
//  StationManager
//
//  Created by 张佳强 on 17/3/13.
//  Copyright © 2017年 Helong. All rights reserved.
//

#import "CommitTaskViewController.h"
#import "ListTableView.h"
#import "DataHandel.h"
#import "UserInfoModel.h"
#import "StationInfoModel.h"
#import <MBProgressHUD.h>

@interface CommitTaskViewController ()

//背景
@property (nonatomic,strong) UIView * bgView;
//维护任务类型
@property (nonatomic,strong) UILabel * taskType;
//弹出提示
@property (strong,nonatomic) MBProgressHUD * hud;
//类型选择按钮
@property (nonatomic,strong) UIButton * typeButton;
//备注
@property (nonatomic,strong) UILabel * remarkLabel;
//备注输入框
@property (nonatomic,strong) UITextView * textView;
//提交按钮
@property (nonatomic,strong) UIButton * commitButton;
//弹出菜单
@property (nonatomic,strong) ListTableView * listView;
@property (nonatomic,strong) UIView * bottomView;
@property (nonatomic,strong) NSString * stationNums;
@end

@implementation CommitTaskViewController


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setButtonTitle:) name:@"setButtonTitle" object:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"下达维护任务";
    [self setUI];        
}

//不用时移除通知
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"setButtonTitle" object:nil];
}

#pragma mark Notification - Delegate
//更改按钮文字
- (void)setButtonTitle:(NSNotification *)notify {
    
    NSString * title = (NSString *)notify.object;
    [_typeButton setTitle:title forState:UIControlStateNormal];
    [_listView removeFromSuperview];
    _listView = nil;
    [_bgView removeFromSuperview];
    _bgView = nil;
}

//搭建UI
- (void)setUI {
    
    
    _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-104, SCREEN_WIDTH, 40)];
    _bottomView.backgroundColor = [UIColor colorWithRed:34/255.0 green:150/255.0 blue:243/255.0 alpha:1.0];
    [self.view addSubview:_bottomView];

    
    _commitButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_commitButton setTitle:@"提交" forState:UIControlStateNormal];
    [_commitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_commitButton addTarget:self action:@selector(commitClick) forControlEvents:UIControlEventTouchUpInside];
    [_bottomView addSubview:_commitButton];
    
    _taskType = [[UILabel alloc]init];
    _taskType.text = @"维护任务类型:";
    _taskType.font = [UIFont systemFontOfSize:13.0];
    _taskType.textAlignment = NSTextAlignmentCenter;
    _taskType.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_taskType];
    
    _typeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_typeButton setTitle:@"巡检" forState:UIControlStateNormal];
    [_typeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _typeButton.titleLabel.font = [UIFont systemFontOfSize:13.0];
    _typeButton.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1.0];
    _typeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    _typeButton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    _typeButton.adjustsImageWhenHighlighted = NO;
    [_typeButton addTarget:self action:@selector(typeClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_typeButton];
    
    UIImageView * arrowImageView = [[UIImageView alloc]init];
    arrowImageView.image = [UIImage imageNamed:@"jiantou1"];
    [_typeButton addSubview:arrowImageView];
    
    _remarkLabel = [[UILabel alloc]init];
    _remarkLabel.backgroundColor = [UIColor clearColor];
    _remarkLabel.text = @"  运维原因及备注:";
    _remarkLabel.font = [UIFont systemFontOfSize:13.0];
    [self.view addSubview:_remarkLabel];
    
    _textView = [[UITextView alloc]init];
    _textView.layer.borderWidth = 2;
    _textView.layer.borderColor = [[UIColor orangeColor] CGColor];
    [self.view addSubview:_textView];
    
    [_commitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@-20);
        make.top.equalTo(@5);
        make.bottom.equalTo(@-5);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(30);
    }];

    [_taskType mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.top.equalTo(@10);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(40);
    }];
    
    [_typeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_taskType.mas_right).offset(5);
        make.top.equalTo(@10);
        make.right.equalTo(@-10);
        make.height.mas_equalTo(40);
    }];
    
    [arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@0);
        make.top.equalTo(@10);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];
    
    [_remarkLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.top.equalTo(_taskType.mas_bottom).offset(5);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(40);
    }];
    
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.top.equalTo(_remarkLabel.mas_bottom).offset(5);
        make.right.equalTo(@-10);
        make.bottom.equalTo(@-120);
    }];
}

//点击提交
- (void)commitClick {
    
    _stationNums = @"";
    for (StationInfoModel * model in [self.selectedDict allValues]) {
        _stationNums = [_stationNums stringByAppendingString:[NSString stringWithFormat:@"%@|",model.stationNum]];
    }
    NSData *userData = [USER_DEFAULT objectForKey:@"userInfoModel"];
    UserInfoModel *userModel = [NSKeyedUnarchiver unarchiveObjectWithData:userData];
    NSString * userID = userModel.UserID;
    if (_stationNums.length>4) {
        [[DataHandel sharedDataHandel] addMaintainTaskWithStationNum:_stationNums userID:userID taskType:self.typeButton.titleLabel.text remarks:self.textView.text Block:^(id object) {
      
    }];
    }
    [self showAlert:@"已提交" duration:0.8];
    
}
//弹出提示框
- (void)showAlert:(NSString *)message duration:(CGFloat)duration {
    
    _hud = [[MBProgressHUD alloc] init];
    _hud.center = CGPointMake(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
    [[UIApplication sharedApplication].keyWindow addSubview:_hud];
    _hud.animationType =MBProgressHUDAnimationZoom;
    _hud.mode = MBProgressHUDModeText;
    _hud.label.text = message;
    _hud.removeFromSuperViewOnHide = YES;
    [_hud showAnimated:NO];
    [_hud hideAnimated:YES afterDelay:duration];
    [self.navigationController popViewControllerAnimated:YES];
    
}
//点击类型选择按钮
- (void)typeClick {
    
    _bgView = [[UIView alloc]initWithFrame:self.view.bounds];
    _bgView.backgroundColor = [UIColor clearColor];
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hide)];
    [_bgView addGestureRecognizer:tap];
    [self.view addSubview:_bgView];
    
    _listView = [[ListTableView alloc]initWithFrame:CGRectMake(_typeButton.frame.origin.x, _typeButton.frame.origin.y+40, SCREEN_WIDTH-115, 80) style:UITableViewStylePlain];
    _listView.dataArray = @[@"巡检",@"紧急"];
    _listView.vcName = @"Commit";
    [_listView reloadData];
    [self.view addSubview:_listView];
    
}

//隐藏背景
- (void)hide {
    
    [_listView removeFromSuperview];
    _listView = nil;
    [_bgView removeFromSuperview];
    _bgView = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
